<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*荣耀健身跑步机*/
class HonourTreadmill extends  BaseApi
{
	/**
	 * 测试地址
	 * */
	 public function _initialize()
    {
    }
	function _card_before_method($params){
		if(strpos($params,"token")===FALSE){
			$this->card_ajax_error("参数错误");
		}
		if(strpos($params,"sign")===FALSE){
			$this->card_ajax_error("参数错误");
		}
		if(strpos($params,"time")===FALSE){
			$this->card_ajax_error("参数错误");
		}
		$params=json_decode($params,TRUE);
		if(empty($params)){
			$this->card_ajax_error("参数格式错误");
		}else{
			if(!isset($params['debug'])){
				if($params['time']!=1){
					if(($params['time']+300)<time()||($params['time']-300)>time()){//接口调用时间操过5分钟
						return $this->card_ajax_error("链接已经过期");
					}	
				}
				if($this->_get_sign($params['user_id'],$params['time'],$params['token'])!=$params['sign']){
					$this->card_ajax_error("签名错误");
				}	
			}
			
		}
		return $params;
	}
	function _data_before_method($params){
		if(strpos($params,"token")===FALSE){
			$this->data_ajax_error("参数错误");
		}
		if(strpos($params,"sign")===FALSE){
			$this->data_ajax_error("参数错误");
		}
		if(strpos($params,"time")===FALSE){
			$this->data_ajax_error("参数错误");
		}
		$params=json_decode($params,TRUE);
		if(empty($params)){
			$this->data_ajax_error("参数格式错误");
		}else{
			if(!isset($params['debug'])){
				if($params['time']!=1){
					if(($params['time']+300)<time()||($params['time']-300)>time()){//接口调用时间操过5分钟
						return $this->data_ajax_error("链接已经过期");
					}	
				}
				if($this->_get_sign($params['user_id'],$params['time'],$params['token'])!=$params['sign']){
					$this->data_ajax_error("签名错误");
				}
			}
		}
		return $params;
	}
	/**
	 * 验证卡
	 * http://smartgym.com/HonourThreadmill/card
	 * */
	function card(){
		if(request()->isPost()){
			$params=file_get_contents("php://input");
			$params=$this->_card_before_method($params);
			if(empty($params)){
				$this->card_ajax_error("参数格式错误");
			}else{
				if(isset($params['user_id'])){
					$count=db('Interface')->where("shop_id=".$params['token']." and card_no='".$params['user_id']."'")->count(1);
					if($count<1){
						$this->card_ajax_error("会员不存在");
					}else{
						$this->card_ajax_success();
					}
				}else{
					$this->card_ajax_error("user_id不存在！");					
				}
			}
		}else{
			$this->card_ajax_error("非法访问！");
		}
	}
	/**
	 * 上传数据
	 * */ 
	function data(){
		if(request()->isPost()){
			$params=file_get_contents("php://input");
			$params=$this->_data_before_method($params);
			if(empty($params)){
				$this->data_ajax_error("参数格式错误");
			}else{
				if(isset($params['user_id'])){
					db('SportTreadmill')->insert(array(
						"card_no"=>$params['user_id'],
						"shop_id"=>$params['token'],
						"start_time"=>$params['start_time'],
						"end_time"=>$params['end_time'],
						"run_distance"=>$params['run_distance'],
						"avg_speed"=>$params['avg_speed'],
						"avg_incline"=>$params['avg_incline'],
						"burn_cal"=>$params['burn_cal'],
						"heart_rate"=>$params['heart_rate'],
						"sport_time"=>$params['sport_time'],
						"pace"=>$params['pace'],
						"mac"=>$params['mac'],
					));
						$this->data_ajax_success();
				}else{
					$this->data_ajax_error("user_id不存在！");					
				}
			}
		}else{
			$this->card_ajax_error("非法访问！");
		}
	} 
	function test(){
		$this->card_ajax_error($this->_get_sign(input("user_id"),input("time"),input("token")));
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function card_ajax_success(){
		header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode(array('card'=>"0","err_msg"=>""),JSON_UNESCAPED_UNICODE));die;
	}
	function card_ajax_error($msg=""){
		header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode(array('card'=>"-1","err_msg"=>$msg),JSON_UNESCAPED_UNICODE));die;
	}
	
	function data_ajax_success(){
		header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode(array('upload_data'=>"0","err_msg"=>""),JSON_UNESCAPED_UNICODE));die;
	}
	function data_ajax_error($msg=""){
		header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode(array('upload_data'=>"-1","err_msg"=>$msg),JSON_UNESCAPED_UNICODE));die;
	}
	function _get_sign($user_id,$time,$token){
		return md5($user_id."_".$time."_".$token);
	}
}
